//
//  FightingLayer.cpp
//  DiceWar
//
//  Created by xiaofeng on 27/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "FightingLayer.h"
#include "GameMaster.h"
#include "GameConstants.h"
#include "RandomGenerator.h"

USING_NS_CC;

bool FightingLayer::init()
{
    if (!CCLayer::init()) return false;
    
    m_pAttackerSprite = createSelectionSprite();
    CCAssert(m_pAttackerSprite, "Failed to create the Attacker Sprite!");
    if (!m_pAttackerSprite) return false;
    
    m_pDefenderSprite = createSelectionSprite();
    CCAssert(m_pDefenderSprite, "Failed to create the Defender Sprite!");
    if (!m_pDefenderSprite) return false;
    
    return true;
}

void FightingLayer::setAttacker(int coordinateX, int coordinateY)
{    
    if (!HexCellCoordinate::isValid(coordinateX, coordinateY)) return;
    const HexMap& gameMap = GameMaster::instance().getHexMap();
    const CCPoint kPosition = gameMap.getMapGridPosition(coordinateX, coordinateY);
    
    showAttacker(kPosition);
    m_attackerCoordinate.setCoordinate(coordinateX, coordinateY);   
}

void FightingLayer::setDefender(int coordinateX, int coordinateY)
{
    if (!HexCellCoordinate::isValid(coordinateX, coordinateY)) return;
    const HexMap& gameMap = GameMaster::instance().getHexMap();
    const CCPoint kPosition = gameMap.getMapGridPosition(coordinateX, coordinateY);
    
    showDefender(kPosition);
    m_defenderCoordinate.setCoordinate(coordinateX, coordinateY);
}

bool FightingLayer::isNextToAttacker(int coordinateX, int coordinateY)
{
    HexCellCoordinate coordinate(coordinateX, coordinateY);
    return m_attackerCoordinate.isNextTo(coordinate);
}

bool FightingLayer::isNextToDefender(int coordinateX, int coordinateY)
{
    HexCellCoordinate coordinate(coordinateX, coordinateY);
    return m_defenderCoordinate.isNextTo(coordinate);
}

void FightingLayer::unsetAttacker()
{
    hideAttacker();
    m_attackerCoordinate.setToInvalid();
}

void FightingLayer::unsetDefender()
{
    hideDefender();
    m_defenderCoordinate.setToInvalid();
}

int FightingLayer::getAttackerPower()
{
    CCAssert(m_attackerCoordinate.isValid(), "Attacker is not set!");
    HexMap& hexMap = GameMaster::instance().getHexMap();
    const HexMapGrid& kAttackerGrid = hexMap.getMapGrid(m_attackerCoordinate.getX(), m_attackerCoordinate.getY());
    const int kAttackerNumDices = kAttackerGrid.getNumDices();
    return getPower(kAttackerNumDices);
}

int FightingLayer::getDefenderPower()
{
    CCAssert(m_defenderCoordinate.isValid(), "Defender is not set!");   
    HexMap& hexMap = GameMaster::instance().getHexMap();
    const HexMapGrid& kDefenderGrid = hexMap.getMapGrid(m_defenderCoordinate.getX(), m_defenderCoordinate.getY());
    const int kDefenderNumDices = kDefenderGrid.getNumDices();
    return getPower(kDefenderNumDices);
}

int FightingLayer::getPower(int numDices)
{
    int power = 0;
    RandomGenerator random;
    for (int count = 1; count <= numDices; count++)
    {
        power += random.getRandomNum(6) + 1;
    }
    return power;
}

void FightingLayer::showAttacker(cocos2d::CCPoint position)
{
    m_pAttackerSprite->setIsVisible(true);
    m_pAttackerSprite->setPosition(position);
}

void FightingLayer::hideAttacker()
{
    m_pAttackerSprite->setIsVisible(false);
}

void FightingLayer::showDefender(cocos2d::CCPoint position)
{
    m_pDefenderSprite->setIsVisible(true);
    m_pDefenderSprite->setPosition(position);
}

void FightingLayer::hideDefender()
{
    m_pDefenderSprite->setIsVisible(false);
}

CCSprite* FightingLayer::createSelectionSprite()
{
    CCSprite* pSprite = cocos2d::CCSprite::spriteWithFile("select.png");
    if (!pSprite) return NULL;
    
    const cocos2d::CCSize kSize = pSprite->getContentSize();
    const int kTextureWidth = kSize.width;
    const float kScale = static_cast<float>(gameConstants::kHexGridSize * 2) / static_cast<float>(kTextureWidth);
    pSprite->setScale(kScale);
    pSprite->setIsVisible(false);
    
    this->addChild(pSprite);
    return pSprite;
}